package com.guaju.yueshangLife.activity;

import android.content.Entity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.adapter.DeliveryAdapter;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.bean.OrderDetailBean;
import com.guaju.yueshangLife.databinding.ActivityDiliveryDetailBinding;
import com.guaju.yueshangLife.net.ApiEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class DeliveryDetailActivity extends BaseActivity {


    private ActivityDiliveryDetailBinding binding;

    @Override
    protected void loadViewLayout() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_dilivery_detail);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void bindViews() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent.hasExtra("exp")) {
            String exp = intent.getStringExtra("exp");
            binding.tvDiliveryCompany.setText(exp);
        }
        if (intent.hasExtra("number")) {
            String number = intent.getStringExtra("number");
            binding.tvDiliveryNum.setText(number);
        }

        if (intent.hasExtra("delivery")) {
            List<OrderDetailBean.DataBean.ShipDataBean.DetailBean> deliveryList = (List<OrderDetailBean.DataBean.ShipDataBean.DetailBean>) intent.getSerializableExtra("delivery");
            DeliveryAdapter deliveryAdapter = new DeliveryAdapter(DeliveryDetailActivity.this, deliveryList);
            binding.recyclerview.setAdapter(deliveryAdapter);
        }
        if (intent.hasExtra("orderId")) {
            /**
             * 从订单列表而来
             */
            int orderId = intent.getIntExtra("orderId", 0);
            ApiEntity.orderDetail(orderId);
        } else {
            showEmpty();
        }
    }

    @Override
    protected void setListener() {

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnDeliveryReceived(OrderDetailBean bean) {
        if (bean == null) {
            showEmpty();
        } else {
            OrderDetailBean.DataBean data = bean.getData();
            if (data == null) {
                showEmpty();
            } else {
                OrderDetailBean.DataBean.ShipDataBean ship_data = data.getShip_data();
                if (ship_data == null) {
                    showEmpty();
                } else {
                    String exp = ship_data.getExp();
                    String exp_no = ship_data.getExp_no();
                    List<OrderDetailBean.DataBean.ShipDataBean.DetailBean> detail = ship_data.getDetail();
                    if (TextUtils.isEmpty(exp) || TextUtils.isEmpty(exp_no) || detail.isEmpty()) {
                        showEmpty();
                    } else {
                        binding.tvDiliveryNum.setText(exp_no);
                        binding.tvDiliveryCompany.setText(exp);
                        DeliveryAdapter deliveryAdapter = new DeliveryAdapter(DeliveryDetailActivity.this, detail);
                        binding.recyclerview.setAdapter(deliveryAdapter);
                    }
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showEmpty() {
        binding.emptyview.getRoot().setVisibility(View.VISIBLE);
        binding.emptyview.tvOrderEmpty.setText("暂无物流信息");
        binding.emptyview.tvGoIndex.setVisibility(View.GONE);
    }
}
